package com.shierke.Ume.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.google.gson.Gson
import com.shierke.Ume.api.ApiService
import com.shierke.Ume.bean.LoginObject
import com.shierke.Ume.bean.LoginOutObject
import com.shierke.Ume.bean.SMScallObject
import com.shierke.Ume.bean.SendCodeObject
import com.shierke.Ume.util.asMutableLiveData
import com.ume.supplier.cn.httputil.http.HttpCallback
import com.ume.supplier.cn.httputil.http.HttpHelper

/**
 * Create by Ybx
 * on 2020-11-10
 * Desribe:
 */
class RegisterViewModel : ViewModel() {


    val sendCodeBean: LiveData<SendCodeObject> by lazy { MutableLiveData<SendCodeObject>() }
    val loginOutBean: LiveData<LoginOutObject> by lazy { MutableLiveData<LoginOutObject>() }
    val loginBean: LiveData<LoginObject> by lazy { MutableLiveData<LoginObject>() }
    val smsBean: LiveData<SMScallObject> by lazy { MutableLiveData<SMScallObject>() }


    fun sendCode(phoneNum: String, region: String, type: String) {
        HttpHelper.instance.request(
            ApiService.retrofitApis.postSendCode(
                HttpHelper.createBody(
                    HttpHelper.gson.toJson(
                        HttpHelper.getParams()
                            .put("account", phoneNum)
                            .put("region", region)
                            .put("token", "")
                            .put("type", type).build()
                    )
                )
            ),
            object : HttpCallback<SendCodeObject>() {
                override fun onSuccess(response: SendCodeObject) {
                    sendCodeBean.asMutableLiveData().value = response
                }

                override fun onFailure(message: String?, err: Throwable) {
                    sendCodeBean.asMutableLiveData().value = null
                }

                override fun onRequestError(message: String, code: Int) {
                    super.onRequestError(message, code)
                }

            }, false
        )
    }


    fun logOut() {
        HttpHelper.instance.request(
            ApiService.retrofitApis.logOut(),
            object : HttpCallback<LoginOutObject>() {
                override fun onSuccess(response: LoginOutObject) {
                    loginOutBean.asMutableLiveData().value = response
                }

                override fun onFailure(message: String?, err: Throwable) {
                    loginOutBean.asMutableLiveData().value = null

                }

            }, false
        )
    }


    fun login(phoneNum: String, region: String, type: String, smsCode: String) {
        HttpHelper.instance.request(
            ApiService.retrofitApis.login(
                HttpHelper.createBody(
                    HttpHelper.gson.toJson(
                        HttpHelper.getParams()
                            .put("account", phoneNum)
                            .put("loginType", type)
                            .put("region", region)
                            .put("smsCode", smsCode)
                            .build()
                    )
                )
            ),
            object : HttpCallback<LoginObject>() {
                override fun onSuccess(response: LoginObject) {
                    loginBean.asMutableLiveData().value = response
                }

                override fun onFailure(message: String?, err: Throwable) {
                    loginBean.asMutableLiveData().value = null
                }

            }, false
        )
    }


    fun smsCall() {

        HttpHelper.instance.request(
            ApiService.retrofitApis.smsCall(
                HttpHelper.getParams()
                    .put("code", "")
                    .put("destId", "")
                    .put("phone", "")
                    .put("result", "")
                    .put("smsid", "")
                    .put("spid", "")
                    .put("usersmsid", "")
                    .build()
            ),
            object : HttpCallback<SMScallObject>() {
                override fun onSuccess(response: SMScallObject) {
                    smsBean.asMutableLiveData().value = response
                }

                override fun onFailure(message: String?, err: Throwable) {
                    smsBean.asMutableLiveData().value = null
                }

            }, false
        )
    }

}